<?php
/*!
 * HarmonyCSS - CSS minify and optimize library
 * 
 * Copyright 2010, Anton Ilyuschenkov aka DriverX
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://driverx.su/license.html
 */

define('HCSS_ATRULE_PREFIX', '@');
define('HCSS_BEGIN_BLOCK', '{');
define('HCSS_END_BLOCK', '}');
define('HCSS_PROP_DELIMITER', ';');
define('HCSS_PROP_VALUE_DELIMITER', ':');
define('HCSS_BEGIN_HEX_COLOR', '#');
define('HCSS_STRING_1QUOTES', '\'');
define('HCSS_STRING_2QUOTES', '"');
define('HCSS_COMMENT_BEGIN', '/*');
define('HCSS_COMMENT_END', '*/');
define('HCSS_PARSE_STATE_SEARCH', 1);
define('HCSS_PARSE_STATE_ATRULE', 2);
define('HCSS_PARSE_STATE_IN_BLOCK', 3);
define('HCSS_PARSE_STATE_SELECTOR', 4);
define('HCSS_PARSE_STATE_PROPERTY', 5);
define('HCSS_PARSE_STATE_VALUE', 6);
define('HCSS_PARSE_STATE_STRING', 7);
define('HCSS_PARSE_STATE_COMMENT', 8);

class HarmonyCSS {
	public $source;
	public $sourceOptimized;
	public $options = array();
	public $at = array();
	public $fragments = array();
	protected $parseState; 
	public function __construct ($source, $options = null) {
		if (!$source) {
			throw new ErrorException('source is empty');
		}
		if (!is_string($source)) {
			throw new ErrorException('source is not string');
		}
		$this->source = $this->sourceOptimized = $source;
	}
	public function isURI (&$str) {
		return !!preg_match('/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/', $str);
	}
	public function addFragment ($media, $selector, $property, $value) {
		$this->fragments[] = array(
			$media,
			array(
				array(
					$selector,
					array($property, $value)
				)
			)
		);
	}
	public function addAt ($rule, $any, $block) {
		
	}
	public function optimize () {
		$source = &$this->sourceOptimized;
		$source = str_replace("\t", ' ', $source);
		$source = str_replace("\r\n", "\n", $source);
		$source = ' '.$source.' ';
		$source = str_replace('  ', ' ', $source);
		$this->parseState = HCSS_PARSE_STATE_SEARCH;
		$state = &$this->parseState;
		
		for ($i = 0, $len = strlen($source); $i < $len; $i++) {
			$symbol = $source{$i};
			switch ($state) {
				case HCSS_PARSE_STATE_SEARCH:
					if ($symbol === HCSS_ATRULE_PREFIX) {
						$state = HCSS_PARSE_STATE_ATRULE;
					} else if ($symbol === '/' && $symbol.$source{$i + 1} === HCSS_COMMENT_BEGIN) {
						$state = HCSS_PARSE_STATE_COMMENT;
						$i++;
					}
				break;
				case HCSS_PARSE_STATE_COMMENT:
					if ($source{$i + 1} === '/' && $symbol === '*') {
						$state = HCSS_PARSE_STATE_SEARCH;
					}
				break;
				case HCSS_PARSE_STATE_ATRULE:
					if ($symbol === ';' || $symbol === HCSS_END_BLOCK) {
						$state = HCSS_PARSE_STATE_SEARCH;
					}
				break;
			}
		}
	}
	public function postparse () {
		
	}
	public function write () {
		echo $this->sourceOptimized;
	}
}
class HarmonyCSSOptimizer {
	public function selector () {
		
	}
	public function value () {
		
	}
	public function mergeSelectors () {
		
	}
}

$cssminify = new HarmonyCSS(file_get_contents('./tests/test1.css'));
$cssminify->optimize();
$cssminify->write();

$test = array();
// 13 10
/*
$j = 1;
for ($i = 1000; $i < 1010; $i++) {
	$test[] = $j % 2 ? 10 | (2 << (3 + $j)) : 13 | (2 << (3 + $j));
	$j++;
}
*/
// 001010101010
// 110101010101
$j = 1;
$tmp10 = 10;
$tmp13 = 13;
for ($i = 1000; $i < 1010; $i++) {
	if ($j % 2) {
		$tmp10 = $tmp10 << 2 | 2;
		$test[] = $tmp10;
	} else {
		$tmp13 = $tmp13 << 2 | 1;
		$test[] = $tmp13;
	}
	$j++;
}

echo "<pre>AND\n";
for ($i = 0; $i < count($test); $i++) {
	echo $test[$i]."\t";
	for ($j = 0; $j < count($test); $j++) {
		echo "|".($test[$i]  & $test[$j])."\t"; 
	}
	echo "\n";
}
echo "\nOR\n";
for ($i = 0; $i < count($test); $i++) {
	echo $test[$i]."\t";
	for ($j = 0; $j < count($test); $j++) {
		echo "|".($test[$i] | $test[$j])."\t"; 
	}
	echo "\n";
}
echo "</pre>";

?>